package com.zahb.qadx.ui.fragment;


import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager2.widget.ViewPager2;

import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.zahb.qadx.R;
import com.zahb.qadx.base.BaseApplication;
import com.zahb.qadx.model.MainFunc1;
import com.zahb.qadx.model.Mission24;
import com.zahb.qadx.ui.adapter.ByLanguageAdapter;
import com.zahb.qadx.ui.adapter.ByLanguageDialogAdapter;
import com.zahb.qadx.ui.view.PlayDialog;
import com.zahb.qadx.util.CompatHelper;
import com.zahb.qadx.util.DisplayUtil;
import com.zhpan.bannerview.BannerViewPager;
import com.zhpan.bannerview.constants.PageStyle;
import com.zhpan.indicator.enums.IndicatorStyle;

import org.jetbrains.annotations.NotNull;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

import static android.content.Context.MODE_PRIVATE;

//宣传标语
//2021 .3/11 LEB
public class SloganFragment extends HomeFuncFragment {
    static final String TAG = "SloganFragment";
    private PlayDialog mMyDialog;
    private ImageView cancel;
    public   MainFunc1 mainFunc1;
    HomeFragment homeFragment;
    private RelativeLayout according_to_disappear;
    private BannerViewPager<Mission24.SafetySloganListBean, ByLanguageAdapter.BannerViewHolder> mBannerView;
    private BannerViewPager<Mission24.SafetySloganListBean, ByLanguageDialogAdapter.BannerViewHolder> mBannerViewMyDialog;
    static SloganFragment newInstance(MainFunc1 mainFunc1) {
        Bundle args = new Bundle();
        args.putSerializable("data", mainFunc1);
        SloganFragment fragment = new SloganFragment();
        fragment.setArguments(args);
        return fragment;
    }
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    public View onCreateView(@NotNull LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment

         View view= inflater.inflate(R.layout.fragment_slogan, container, false);
        according_to_disappear=view.findViewById(R.id.according_to_disappear);
        mainFunc1 = (MainFunc1) mArguments.getSerializable("data");
        initBannerView(view);
                if (mainFunc1!=null){
                    mBannerView.refreshData(mainFunc1.getSlogan().getSafetySloganList());
                    mBannerViewMyDialog.refreshData(mainFunc1.getSlogan().getSafetySloganList());
                }


        return view;

    }

    @Override
    public void onResume() {
        super.onResume();
        if (getParentFragment() instanceof HomeFragment) {
            homeFragment = (HomeFragment) getParentFragment();

        }
    }

    @NonNull
    @Override
    protected RecyclerView.LayoutManager getLayoutManager() {
        return null;
    }

    @Nullable
    @Override
    protected RecyclerView.ItemDecoration getItemDecoration() {
        return null;
    }

    @NonNull
    @Override
    protected RecyclerView.Adapter<BaseViewHolder> getAdapter() {
        return null;
    }

    @Override
    protected void seeMore() {
    }


    // 初始化轮播控件
    private void initBannerView(View rootView) {
        View view = getLayoutInflater().inflate(R.layout.openingthefigure, null);
        mMyDialog = new PlayDialog(getActivity(), 1000, 0, view, R.style.DialogTheme);
//        mMyDialog.setCanceledOnTouchOutside(false);
        mMyDialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
            @Override
            public void onCancel(DialogInterface dialogInterface) {
                mBannerView.startLoop();
                mBannerView. setAutoPlay(open);
                homeFragment.mBannerView.startLoop();
            }
        });
        cancel=view.findViewById(R.id.cancel);
        cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mMyDialog.dismiss();
                mBannerView. startLoop();
                homeFragment.mBannerView.startLoop();
            }
        });
        mBannerViewMyDialog=view.findViewById(R.id.mBannerViewMyDialog);
        mBannerView = rootView.findViewById(R.id.banner_view);
        mBannerView.setOnPageClickListener(new BannerViewPager.OnPageClickListener() {
            @Override
            public void onPageClick(int position) {
                mBannerViewMyDialog.setCurrentItem(position);
                mBannerView.stopLoop();
                mMyDialog.show();
                homeFragment.mBannerView.stopLoop();
            }
        });

        mBannerView.setOrientation(ViewPager2.ORIENTATION_HORIZONTAL)
                .setAutoPlay(true)
                .setInterval(3000)
                .setScrollDuration(300)
                .setLifecycleRegistry(getLifecycle())
                .setIndicatorStyle(IndicatorStyle.CIRCLE)
                .setPageStyle(PageStyle.MULTI_PAGE_SCALE)
                .setIndicatorSliderRadius(DisplayUtil.dip2px(3f))
                .setIndicatorSliderColor(CompatHelper.getColor(R.color.gray_a1a1a1), CompatHelper.getColor(R.color.blue_409eff))
                .setAdapter(new ByLanguageAdapter(getActivity(),mainFunc1.getSlogan()))
                .create();

        mBannerViewMyDialog.setOrientation(ViewPager2.ORIENTATION_HORIZONTAL)
                .setAutoPlay(true)
                .setInterval(3000)
                .setScrollDuration(300)
                .setLifecycleRegistry(getLifecycle())
                .setIndicatorStyle(IndicatorStyle.DASH)
                .setIndicatorSliderRadius(DisplayUtil.dip2px(3f))
                .setIndicatorSliderColor(CompatHelper.getColor(R.color.gray_a1a1a1), CompatHelper.getColor(R.color.blue_409eff))
                .setAdapter(new ByLanguageDialogAdapter(getActivity()))
                .create();


         //  宣传标语是否开屏
        if (mainFunc1.getSlogan().isOpenScreenStatus()){
            isTodayFirstShow();
        }
        if (mainFunc1.getSlogan().isHomePageStatus()){

        }else {
            according_to_disappear.setVisibility(View.GONE);
        }

    }
     //宣传语开屏 每天一次
    private void isTodayFirstShow() {
        SharedPreferences pref = getActivity().getSharedPreferences("OpeningTheDaily", MODE_PRIVATE);
        SharedPreferences.Editor editor = pref.edit();
        int id = BaseApplication.getContext().getDataLogin().getUser().getId();
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd", Locale.CHINA);
        String lastTime = pref.getString("date_" + id, "");
        try {
            String currentTime = dateFormat.format(new Date());
            if (!TextUtils.equals(lastTime, currentTime)) {
                mMyDialog.show();
                mBannerView.startLoop();
                editor.putString("date_" + id, currentTime).apply();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}
